import { prisma } from "@/lib/prisma"
import { InboundList } from "@/components/inbound/inbound-list"
import { InboundHeader } from "@/components/inbound/inbound-header"
import { Metadata } from "next"
import { Purchase, Supplier } from "@prisma/client"

export const metadata: Metadata = {
  title: "入库管理 - 商场销售系统",
  description: "入库管理页面",
}

// 序列化 Decimal 类型
function serializePurchase(purchase: Purchase & { 
  supplier: Supplier, 
  items: any[] 
}) {
  return {
    ...purchase,
    totalAmount: purchase.totalAmount.toString(),
    items: purchase.items.map(item => ({
      ...item,
      price: item.price.toString(),
      amount: item.amount.toString(),
      product: {
        ...item.product,
        price: item.product.price.toString()
      }
    }))
  }
}

export default async function InboundPage() {
  const purchases = await prisma.purchase.findMany({
    orderBy: {
      createdAt: 'desc'
    },
    include: {
      supplier: true,
      items: {
        include: {
          product: true
        }
      }
    }
  })

  // 序列化所有入库单数据
  const serializedPurchases = purchases.map(serializePurchase)

  return (
    <div className="space-y-4 p-8">
      <InboundHeader />
      <InboundList purchases={serializedPurchases} />
    </div>
  )
} 